﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.Data;
using PokeData;
using Microsoft.Xna.Framework;

namespace PokeTD
{
    class TowerDetail
    {
        private int _towerId;
        private Texture2D _towerTexture;
        private bool buymode = false;
        private int _cost;
        private int _upgradeCost;

        public int TowerId
        {
            get { return _towerId; }
            set { _towerId = value; }
        }
        public Texture2D TowerTexture
        {
            get { return _towerTexture; }
            set { _towerTexture = value; }
        }
        public int Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }
        public int UpgradeCost {
            get { return _upgradeCost; }
            set { _upgradeCost = value; }
        }

        private DataRow towerrow;
        private Vector2 RenderRoot;
        private SpriteFont[] fonts;
        private Vector2 TextRenderRoot;
        private Vector2 lineHeight = new Vector2(0,20);
        private DataRow uptowerrow;
        private bool canUpgrade;
        private List<Texture2D> TypeTextures;

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="TID">ID des Towers</param>
        /// <param name="towertex">Dictionary, in dem nach einer passenden Textur für den Tower gesucht wird</param>
        /// <param name="RenderRoot">Vector2 zur Linken oberen Ecke des Detailfensters</param>
        /// <param name="buy">Gibt an, ob der Preis angezeigt wird oder nicht.</param>
        public TowerDetail(int TID, Dictionary<string, Texture2D> towertex, Vector2 renderRoot, SpriteFont[] fnts, List<Texture2D> tt, bool buy)
        {
            IEnumerable<DataRow> q = from a in Globals.waveDS.Towers
                                     where a.id == TID
                                     select a;
            towerrow = q.First<DataRow>();

            _towerTexture = towertex["buy" + towerrow["id"].ToString()];
            fonts = fnts;
            RenderRoot = renderRoot;
            _cost = (int)towerrow["Cost"];
            TextRenderRoot = RenderRoot + new Vector2(0,_towerTexture.Height + 10);
            buymode = buy;
            TypeTextures = tt;
            if ((int)towerrow["UpgradeTo"] != -1)
            {
                IEnumerable<DataRow> r = from a in Globals.waveDS.Towers
                                         where a.id == (int)towerrow["UpgradeTo"]
                                         select a;
                canUpgrade = true;
                uptowerrow = r.First<DataRow>();
                _upgradeCost = (int)uptowerrow["Cost"];
            }
        }

        public void Draw(SpriteBatch sb, Texture2D pkdollar)
        {
            int lines = 0;
            sb.Draw(_towerTexture, RenderRoot, Color.White);
            sb.DrawString(fonts[0], (string)towerrow["name"], RenderRoot + new Vector2(_towerTexture.Width + 10, 0), Color.Black);
            if (buymode)
            {
                string text = "Preis: " + _cost.ToString() + " ";
                sb.DrawString(fonts[1], text, TextRenderRoot, Color.Black);
                sb.Draw(pkdollar, new Vector2(TextRenderRoot.X + fonts[1].MeasureString(text).X, TextRenderRoot.Y+3), Color.Black);
                    
                lines++;
            }
            //sb.DrawString(fonts[1], "INSERT STATS HERE", TextRenderRoot + lineHeight * lines, Color.Black);
            sb.Draw(TypeTextures[(int)towerrow["Type1"]], TextRenderRoot + lineHeight * lines, Color.White);
            if((int)towerrow["Type2"] != -1 && towerrow["Type1"] != towerrow["Type2"])
                sb.Draw(TypeTextures[(int)towerrow["Type2"]], TextRenderRoot + lineHeight * lines + new Vector2(35,0), Color.White);
            lines++;
            if (!(bool)towerrow["CanAttackAir"] && !(bool)towerrow["CanAttackGround"])
            {
                sb.DrawString(fonts[1], "Kann nicht angreifen", TextRenderRoot + lineHeight * lines, Color.Black);
                lines++;
            }
            if ((bool)towerrow["CanAttackAir"] && !(bool)towerrow["CanAttackGround"])
            {
                sb.DrawString(fonts[1], "Air-Only", TextRenderRoot + lineHeight * lines, Color.Black);
                lines++;
            }
            if (!(bool)towerrow["CanAttackAir"] && (bool)towerrow["CanAttackGround"])
            {
                sb.DrawString(fonts[1], "Ground-Only", TextRenderRoot + lineHeight * lines, Color.Black);
                lines++;
            }
            sb.DrawString(fonts[1], "Angriff:" + towerrow["AttackMin"].ToString() + " - " + towerrow["AttackMax"].ToString(), TextRenderRoot + lineHeight * lines, Color.Black);
            lines++;
            sb.DrawString(fonts[1], "Range:" + towerrow["Range"].ToString(), TextRenderRoot + lineHeight * lines, Color.Black);
            lines++;
            sb.DrawString(fonts[1], towerrow["AttackSpeed"].ToString() + " Angriffe / Sekunde", TextRenderRoot + lineHeight * lines, Color.Black);
            if ((string)towerrow["EffectName"] != "none")
            {
                lines++;
                sb.DrawString(fonts[1], "Effekt: " + towerrow["EffectProbability"].ToString() + "% " + (string)towerrow["EffectName"], TextRenderRoot + lineHeight * lines, Color.Black);
            }
            lines++;
            if (canUpgrade && !buymode)
            {
                string text = "Entwickeln zu " + uptowerrow["name"].ToString() + ": " + _upgradeCost + " ";
                sb.DrawString(fonts[1], text, TextRenderRoot + lineHeight * lines, Color.Black);
                sb.Draw(pkdollar, new Vector2(TextRenderRoot.X + lineHeight.X * lines + fonts[1].MeasureString(text).X, TextRenderRoot.Y + lineHeight.Y * lines + 3), Color.Black);
                lines++;
            }
        }
    }
}
